import React, { useCallback } from 'react';
import { DatePicker } from 'antd';
import { timeRange } from '../utils/timeRange';

const DatePickerComponent = ({
    dateCallback,
    setSecondBool
}) => {

    const onChange = useCallback((date, str) => {
        if (!str) {
            let start = 0, end = Infinity;
            dateCallback({ start, end });
            setSecondBool(false);
        } else {
            let [year, mouth] = str.split('-');

            mouth.split('')?.[0] === '0' && (mouth = mouth.split('')[1]);

            let [start, end] = timeRange(parseInt(year), parseInt(mouth));

            dateCallback({ start, end });
            setSecondBool(true);
        }
    }, [dateCallback, setSecondBool]);

    return <DatePicker onChange={onChange} picker="month" placeholder='选择日期'/>;
};

export default React.memo(DatePickerComponent);